package cn.android_mobile.core.net.http;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import cn.android_mobile.core.enums.RequestType;
import cn.android_mobile.core.enums.ResponseChartset;
import cn.android_mobile.core.net.HttpClientFactory;
import cn.android_mobile.toolkit.Lg;

import com.google.gson.Gson;
/**
 * 
 * 最底层与服务器端交互请求类.<br>
 * @author Administrator <br>
 * @version 1.0.0 2014年11月18日<br>
 * @see 
 * @since JDK 1.5.0
 */
public abstract class Service {
	public RequestType REQUEST_TYPE = RequestType.POST;
	public ResponseChartset CHARSET = ResponseChartset.UTF_8;
	private static final int SET_CONNECTION_TIMEOUT = 60 * 1000;
	private static final int SET_SOCKET_TIMEOUT = 60 * 1000;
	protected DefaultHttpClient client = null;
	protected HttpGet requestGet = null;
	protected HttpPost requestPost = null;

	protected abstract ServiceResponse execute(ServiceRequest request);

	protected Gson g = new Gson();

	private Http http = null;

	public void cancel() {
		if (requestGet != null && requestGet.isAborted() == false) {
			requestGet.abort();
		}
		if (requestPost != null && requestPost.isAborted() == false) {
			requestPost.abort();
		}
		if (http != null) {
			http.cancel();
		}
		// if (client != null) {
		// client.getConnectionManager().shutdown();
		// }
	}

	protected DefaultHttpClient createHttpClient() {
		if (client == null) {
			// client = new DefaultHttpClient();
			client = HttpClientFactory.getHttpClient();
		}
		return client;
	}

	protected List<BasicNameValuePair> getParams(ServiceRequest req) {
		ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		BasicNameValuePair param = null;
		Class<? extends ServiceRequest> c = req.getClass();
		Field[] fields = c.getDeclaredFields();
		for (Field f : fields) {
			try {
				String s = (String) f.get(req);
				param = new BasicNameValuePair(f.getName(), s);
				if (s != null) {
					params.add(param);
				}
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}

		}
		return params;
	}

	protected String request(RequestType requestType, String actionUrl,
			ServiceRequest req) {
		if (!actionUrl.startsWith(ServerUrl.Server)) {
			actionUrl = ServerUrl.Server + actionUrl; 
		}
		http = new Http(actionUrl, Http.POST);
		if (req != null) {
			
		
		List<BasicNameValuePair> valueList = getParams(req);
		 Lg.print("network","URL:"+actionUrl);
		 Lg.print("network","params:"+g.toJson(req));
		if (valueList.size() > 0) {
			for (BasicNameValuePair value : valueList) {
				if (value.getValue().toLowerCase().indexOf("file://") != -1) {
					String[] split = value.getValue().split(";");
					for (String s : split) {
						if(!s.equals("")){
							http.addFile(s.replaceAll("file://", ""));
						}
					}
				} else {
					String[] split = value.getValue().split(";");
					for (String s : split) {
						http.addParam(value.getName(), s);
					}
				}
			}
		}
		}
		String str=http.execute();
		Lg.print("network","response:"+str);
		return str;
	}
	
}
